package com.imjohn.controller;

import com.imjohn.entity.CommentReply;
import com.imjohn.service.CommentReplyService;
import com.imjohn.service.CommentService;
import com.imjohn.util.respUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CommentReplyController {
    @Autowired
    CommentReplyService commentReplyService;
    @Autowired
    CommentService commentService;

    @RequestMapping("/AddCommentReply")
    public Map AddComment ( HttpServletRequest request, @RequestBody CommentReply commentReply ) {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        String userName = (String) session.getAttribute("userName");
        commentReply.setAnswerId(userId);
        commentReply.setAnswerName(userName);
        int i = commentReplyService.AddCommentReply(commentReply);
        int j = commentService.SubReCommentNum(Integer.parseInt(commentReply.getCommentId()));
        if (i == 1 && j == 1) {
            Map map = new HashMap();
            map.put("msg", "success");
            return respUtil.respSuccessHash(map);
        } else {
            String fReason = "评论失败!";
            return respUtil.respFailer(fReason);
        }
    }

    /**
     *
     * 查询子评论
     * @param
     * @return
     */
    @RequestMapping("/queryCommentReply")
    public Map queryCommentReply ( HttpServletRequest request, @RequestBody Map map ) {
        int id = Integer.parseInt(map.get("id").toString());
        List<HashMap> iMap = commentReplyService.queryCommentReply(id);
        if (iMap != null){
            return respUtil.respSuccess(iMap);
        } else {
            String fReason = "系统错误!";
            return respUtil.respFailer(fReason);
        }

    }
}